package club.pinea.handler.game;

import club.pinea.annotations.Head;
import club.pinea.constants.RedisPrefix;
import club.pinea.interfaces.TCPMessage;
import club.pinea.model.PoolScore;
import club.pinea.proto.BasicProto;
import club.pinea.proto.CommandProto;
import club.pinea.proto.GameProto;
import club.pinea.send.MessageSend;
import club.pinea.session.GlobalServerPlayerContainer;
import club.pinea.session.IOSession;
import club.pinea.session.ServerPlayer;
import club.pinea.util.AwardCards;
import club.pinea.util.CardUtil;
import club.pinea.utils.RedisCache;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

@Component
@Head(CommandProto.Command.C_S_REPLACE_CARD_VALUE)
public class ReplaceCardHandler implements TCPMessage<BasicProto.BaseMessage> {

    @Autowired
    private RedisCache redisCache;

    @Override
    public void doMessage(BasicProto.BaseMessage msg, IOSession session) throws Exception {
        GameProto.ReplaceCardRequest request = GameProto.ReplaceCardRequest.parseFrom(msg.getBody());
        ServerPlayer serverPlayer = GlobalServerPlayerContainer.getServerPlayer(session);
        Assert.notNull(serverPlayer.getGameUtil(), "游戏信息不存在");
        List<Integer> stayCards = new ArrayList<>(5);
        stayCards.addAll(request.getCardsList());
//        JSONArray arr = obj.getJSONArray("cards");

        //记录之前保留的牌的数量
        int beginSize = stayCards.size();
        List<Integer> cards = serverPlayer.getGameUtil().getCardsList();

        //给数组对象增加牌
        for (int i = 0; i < 5 - beginSize; i++) {
            stayCards.add(cards.get(5 + i));
        }
        // 设置之后自动更新type
        serverPlayer.getGameUtil().setNowCardList(stayCards);
        int type = serverPlayer.getGameUtil().getType();
        //判断当前手牌的类型
//        int type = CardUtil.getCardType(stayCards);
        int gameScore = CardUtil.scores[serverPlayer.getRoom() - 1][type] * serverPlayer.getGameUtil().getScore();

        if (type >= 7) {
            int score = 0;
            //判断是否是四条以上
            PoolScore poolScore = redisCache.lIndex(RedisPrefix.GAME_POOL_SCORE_PREFIX + serverPlayer.getRoom(), serverPlayer.getSeatId());
            switch (type) {
                case 7:
                    score = poolScore.get_4K();
                    break;
                case 8:
                    score = poolScore.get_SF();
                    break;
                case 9:
                    score = poolScore.get_RS();
                    break;
                case 10:
                    score = poolScore.get_5K();
                    break;
            }
            gameScore += score;

            // TODO 大厅通知
//            redisService.addNotices(
//                    "恭喜房间" + room + " 座位" + seat + " 的用户：" + userName + "获得了" + (type == 7 ? "四条" : type == 8 ? "同花顺" : type == 9 ? "同花大顺" : type == 10 ? "五条" : "") + ",得到彩金：" + score);
        }
        serverPlayer.getGameUtil().setGameScore(gameScore);
        GameProto.ReplaceCardResponse response = GameProto.ReplaceCardResponse.newBuilder()
                .addAllCards(stayCards)
                .setRes(MessageSend.successRes())
                .setType(type)
                .build();
        MessageSend.sendToUser(response, session);
    }
}
